from app.mapper.database import db,UserInfo
# import ast

class UserService:
    def __init__(self):
        pass

    def select_all_users(self):
        users = UserInfo.query.all()
        users_dict_list = [user.to_json() for user in users]
        # print(users)
        # print(type(users))
        return users_dict_list
    
    def checkname(self,username):
        user = UserInfo.query.filter(UserInfo.UserName == username).first()
        # user_template = "{'msg':'{0}','status':'{1}'}"
        if user:
            user_dict = {
                'msg':"success",
                'status':"true"
            }
            return user_dict
        else:
            user_dict = {
                    'msg':"error",
                    'status':"false"
                }
            return user_dict

    def login(self,username,password):
        user = UserInfo.query.filter(UserInfo.UserName == username).first()
        if user is not None:
            select_name = user.UserName
            select_password = user.Password
            if select_name == username and select_password == password:
                return {'msg':"success",'status':"true"}
            else:
                return {'msg':"error",'status':"false"}
        else:
            return {'msg':"error",'status':"false"}
        
    def add_user(self,user):
        try:
            # print(user.to_json())
            db.session.add(user)
            db.session.commit()
            return {"msg":"success","status":"true"}
        except Exception:
            db.session.rollback()
            return {"msg":"error","status":"false"}
    
    def deelete_by_id(self,userid):
        user = UserInfo.query.filter(UserInfo.UserID == userid).first()
        if user:
            try:
                # print(user.to_json())
                db.session.delete(user)
                db.session.commit()
                return {"msg":"success","status":"true"}
            except Exception:
                db.session.rollback()
                return {"msg":"error","status":"false"}
        else:
            return {"msg":"用户不存在","status":"false"}
        

    def edit_user(self,newuser):
        user = UserInfo.query.filter(UserInfo.UserName == newuser.UserName).first()
        if user:
            try:
                user.UserName = newuser.UserName
                user.Password = newuser.Password
                user.Role = newuser.Role
                # db.session.add(newuser)
                db.session.commit()
                return {"msg":"success","status":"true"}
            except Exception:
                db.session.rollback()
                return {"msg":"error","status":"false"}
        else:
            return {"msg":"用户不存在","status":"false"}
        
    def select_all_user(self,page):
        per_page = 10  # 每页显示的数量
        usesrs = UserInfo.query.paginate(page=page, per_page=per_page, error_out=False)
        users_dict_list = [user.to_json() for user in usesrs]

        return users_dict_list